package com.whmall.service.crm.module.supplierdeveloppool.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whmall.component.entity.dto.PageReqDTO;
import com.whmall.component.entity.dto.PageRspDTO;
import com.whmall.service.crm.entity.dto.supplierdevelop.SupplierDevelopPoolFollowAttachmentRspDTO;
import com.whmall.service.crm.entity.dto.supplierdevelop.SupplierDevelopPoolFollowRspDTO;
import com.whmall.service.crm.entity.dto.supplierdevelop.SupplierDevelopPoolFollowSaveReqDTO;
import com.whmall.service.crm.module.supplierdeveloppool.dao.SupplierDevelopPoolFollowAttachmentMapper;
import com.whmall.service.crm.module.supplierdeveloppool.dao.SupplierDevelopPoolFollowMapper;
import com.whmall.service.crm.module.supplierdeveloppool.entity.SupplierDevelopPoolFollowAttachmentDO;
import com.whmall.service.crm.module.supplierdeveloppool.entity.SupplierDevelopPoolFollowDO;
import com.whmall.service.crm.module.supplierdeveloppool.entity.qo.SupplierDevelopPoolFollowQO;
import com.whmall.service.crm.module.supplierdeveloppool.service.SupplierDevelopPoolFollowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SupplierDevelopPoolFollowServiceImpl implements SupplierDevelopPoolFollowService {

    @Autowired
    private SupplierDevelopPoolFollowMapper poolFollowMapper;

    @Autowired
    private SupplierDevelopPoolFollowAttachmentMapper attachmentMapper;

    @Autowired
    private SupplierDevelopPoolFollowValidator validator;

    @Override
    @Transactional
    public Long saveFollow(SupplierDevelopPoolFollowSaveReqDTO saveReqDTO) {

        validator.validate(saveReqDTO);
        SupplierDevelopPoolFollowDO followDO = new SupplierDevelopPoolFollowDO();
        BeanUtils.copyProperties(saveReqDTO, followDO);
        followDO.setCreateId(saveReqDTO.getEmployeeId());
        followDO.setCreateTime(new Date());
        //新增跟进记录
        poolFollowMapper.insertSelective(followDO);

        if (CollUtil.isEmpty(saveReqDTO.getAttachmentUrl())){
            return followDO.getId();
        }
        //添加附件
        saveReqDTO.getAttachmentUrl().forEach(attachmentUrl -> {

            SupplierDevelopPoolFollowAttachmentDO attachmentDO = new SupplierDevelopPoolFollowAttachmentDO();
            attachmentDO.setAttachmentUrl(attachmentUrl);
            //默认不删除
            attachmentDO.setIsDeleted(0);
            attachmentDO.setFollowId(followDO.getId());
            attachmentDO.setCreateId(saveReqDTO.getEmployeeId());
            attachmentDO.setCreateTime(new Date());
            attachmentMapper.insertSelective(attachmentDO);
        });
        return followDO.getId();
    }

    @Override
    public PageRspDTO<SupplierDevelopPoolFollowRspDTO> list(PageReqDTO pageReqDTO, Long companyId) {
        Assert.notNull(companyId, "公司id不能为空");
        //查询结果
        PageInfo<SupplierDevelopPoolFollowDO> pageInfo = PageHelper.startPage(pageReqDTO.getPageNum(),
                pageReqDTO.getPageSize()).doSelectPageInfo(() -> poolFollowMapper.listBy(
                SupplierDevelopPoolFollowQO.builder().companyId(companyId).orderBy(" id desc ").build()));
        if (CollUtil.isEmpty(pageInfo.getList())){
            PageRspDTO<SupplierDevelopPoolFollowRspDTO> pageRspDTO = new PageRspDTO<>();
            pageRspDTO.setPageSize(pageReqDTO.getPageSize());
            pageRspDTO.setPageNum(pageReqDTO.getPageNum());
            return pageRspDTO;
        }

        List<Long> followIds = pageInfo.getList().stream().map(e -> e.getId())
                .collect(Collectors.toList());
        //查询附件信息
        List<SupplierDevelopPoolFollowAttachmentDO> attachmentDOS = attachmentMapper
                .selectByFollowIds(followIds);

        //转换参数
        List<SupplierDevelopPoolFollowRspDTO> followRspDTOList = pageInfo.getList().stream().map(followDO -> {

            SupplierDevelopPoolFollowRspDTO followRspDTO = new SupplierDevelopPoolFollowRspDTO();
            BeanUtils.copyProperties(followDO, followRspDTO);
            attachmentDOS.forEach(attachmentDO -> {

                if (followDO.getId().equals(attachmentDO.getFollowId())) {

                    List<SupplierDevelopPoolFollowAttachmentRspDTO> attachmentRspDTOS =
                            followRspDTO.getAttachmentRspDTOS();
                    if (attachmentRspDTOS == null) {
                        attachmentRspDTOS = new ArrayList<SupplierDevelopPoolFollowAttachmentRspDTO>();
                    }
                    SupplierDevelopPoolFollowAttachmentRspDTO attachmentRspDTO =
                            new SupplierDevelopPoolFollowAttachmentRspDTO();
                    BeanUtils.copyProperties(attachmentDO, attachmentRspDTO);
                    attachmentRspDTOS.add(attachmentRspDTO);
                    followRspDTO.setAttachmentRspDTOS(attachmentRspDTOS);
                }
            });
            return followRspDTO;
        }).collect(Collectors.toList());
        return new PageRspDTO<>(pageInfo.getPageNum(), pageInfo.getPageSize(),
                pageInfo.getPages(), (int) pageInfo.getTotal(), followRspDTOList);
    }

}
